#include "mysql_connection.h"
#include "log.h"

namespace edm{
namespace network{

mysql_connection_t::mysql_connection_t(const string& host,
                 const string& port,
                 const string& usr,
                 const string& pwd,
                 const string& database):
                 m_connected(false),
                 m_host(host),
                 m_port(port),
                 m_usr(usr),
                 m_pwd(pwd),
                 m_database(database)
{
    logtrace((COMMON, "mysql_connection_t::mysql_connection_t host:<%s:%s> db:<%s:%s:%s>", 
              m_host.c_str(), m_port.c_str(), m_usr.c_str(), m_pwd.c_str(), m_database.c_str()));
}
                 
mysql_connection_t::~mysql_connection_t()
{
    logtrace((COMMON, "mysql_connection_t::~mysql_connection_t begin ..."));
    disconnect();
    logtrace((COMMON, "mysql_connection_t::~mysql_connection_t end ok."));
}

int mysql_connection_t::connect()
{
    logtrace((COMMON, "mysql_connection_t::connect begin ..."));
    if(m_connected)
    {
       logwarn((COMMON, "mysql_connection_t::connect already connected.")); 
       return 0;
    }
    
    if(!mysql_init(&m_mysql))
    {
        logerror((COMMON, "mysql_connection_t::connect mysql_init failed:<%s>.", mysql_error(&m_mysql)));
        return -1;    
    }
    
    if(!mysql_real_connect(&m_mysql, 
                            m_host.c_str(), 
                            m_usr.c_str(), 
                            m_pwd.c_str(), 
                            m_database.c_str(), 
                            atoi(m_port.c_str()), NULL, 0))
    {
        logerror((COMMON, "mysql_connection_t::connect mysql_real_connect failed:<%s>.", mysql_error(&m_mysql)));
        return -1;
    }
    
    m_connected = true;
    logtrace((COMMON, "mysql_connection_t::connect end ok."));
    return 0;
}

int mysql_connection_t::disconnect()
{
    logtrace((COMMON, "mysql_connection_t::disconnect begin ..."));
    if(!m_connected)
    {
       logwarn((COMMON, "mysql_connection_t::disconnect already disconnected.")); 
       return 0;
    }
    
    mysql_close(&m_mysql);   
    m_connected = false;
    logtrace((COMMON, "mysql_connection_t::disconnect end ok."));
    return 0;
}

int mysql_connection_t::reconnect()
{
    logtrace((COMMON, "mysql_connection_t::reconnect begin ..."));
    disconnect();
    connect();
    logtrace((COMMON, "mysql_connection_t::reconnect end ok."));
    return 0;    
}

bool mysql_connection_t::connected()
{
    return m_connected;
}

int mysql_connection_t::execute(const string& sql)
{
    logtrace((COMMON, "mysql_connection_t::execute sql=<%s> begin ...", sql.c_str()));
    if(!m_connected)
    {
       logwarn((COMMON, "mysql_connection_t::execute not connected.")); 
       return -1;
    }    
    
    if(mysql_query(&m_mysql,sql.c_str()) != 0)
    {
        logerror((COMMON, "mysql_connection_t::execute sql=<%s> failed:<%s>.", sql.c_str(), mysql_error(&m_mysql)));
        return -1;
    }
    
    logtrace((COMMON, "mysql_connection_t::execute end ok."));    
    return 0;
}

MYSQL_RES* mysql_connection_t::get_result()
{
    return mysql_store_result(&m_mysql);
}

}}


